﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.google.zxing.qrcode;
using System.Drawing;
using com.google.zxing;
using com.google.zxing.common;
using System.Drawing.Imaging;
using System.Text;

namespace CivilleWebRole
{
    public class QRCodeGenerator
    {

        public string generateRandomString()
        {
            StringBuilder str = new StringBuilder(20);
            String dictionary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

            Random rand = new Random((int)DateTime.Now.Ticks);
            for (int i = 0; i < 20; i++)
            {
                str.Append(dictionary[rand.Next(dictionary.Length - 1)]);
            }

            return str.ToString();
        }

        public Bitmap generateQRCode(string code)
        {
            QRCodeWriter writer = new QRCodeWriter();
            ByteMatrix matrix;

            int size = 180;

            matrix = writer.encode(code , BarcodeFormat.QR_CODE, size, size, null);

            Bitmap img = new Bitmap(size, size);
            Color Color = Color.FromArgb(0, 0, 0);

            for (int y = 0; y < matrix.Height; ++y)
            {
                for (int x = 0; x < matrix.Width; ++x)
                {
                    Color pixelColor = img.GetPixel(x, y);

                    //Find the colour of the dot
                    if (matrix.get_Renamed(x, y) == -1)
                    {
                        img.SetPixel(x, y, Color.White);
                    }
                    else
                    {
                        img.SetPixel(x, y, Color.Black);
                    }
                }
            }
            return img;
        }
    }
}